﻿using System;
using FineUI;
using SysManage.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Data.Models.SeekHelp;
using ZHDJ.Data.Models.Donation;
using ZHDJ.PersonalCenter.BLL;
using ZHDJ.WebPage;

namespace ZHDJ.PersonalCenter.Module.DonationAndSeekHelp
{
    public partial class DonAndSeekEdit : PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "DonAndSeekMate";
            }
        }
        #endregion

        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControl();
                BindData();
            }
        }

        #region 绑定控件
        private void BindControl()
        {
            #region 求助
            ControlBusiness.ControlBindDataDictionary(ddlType, SysDataDictionary.DonationType);
            ControlBusiness.ControlBindDataDictionary(ddlState, SysDataDictionary.DonationState);
            #endregion

            #region 捐助
            ControlBusiness.ControlBindDataDictionary(ddlDOType, SysDataDictionary.DonationType);
            ControlBusiness.ControlBindDataDictionary(ddlDOState, SysDataDictionary.DonationState);
            ControlBusiness.ControlBindDataDictionary(ddlDName, SysDataDictionary.DonationDName);
            ControlBusiness.ControlBindDataDictionary(ddlDFrom, SysDataDictionary.DonationDFrom);
            ControlBusiness.ControlBindDataDictionary(ddlPurpose, SysDataDictionary.DonationPurpose);
            #endregion
        }
        #endregion

        #region 绑定数据
        private void BindData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string shid = GetQueryIntValue("shid");
            string doid = GetQueryIntValue("doid");

            #region 求助

            if (shid != "")
            {
                PartySeekHelp model = DB.PartySeekHelps.Find(shid);
                if (model == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                ddlState.SelectedValue = model.State.ToString();
                txtSPerson.Text = model.SPerson;
                txtContactInformation.Text = model.ContactInformation;
                txtAddress.Text = model.Address;
                txtMapCoordinates.Text = model.MapCoordinates;
                ddlType.SelectedValue = model.Type;
                txtName.Text = model.Name;
                txtNum.Text = model.Num.ToString();
                txtEffectiveDate.Text = model.EffectiveDate.ToString();
                txtContent.Text = model.Content;
            }

            #endregion

            #region 捐助
            if (doid != "")
            {
                PartyDonation model = DB.PartyDonations.Find(doid);
                if (model == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                txtDOName.Text = model.Name;
                txtDONum.Text = model.Num.ToString();
                txtDOEffectiveDate.Text = model.EffectiveDate.ToString();
                ddlDFrom.SelectedValue = model.DFrom;
                ddlPurpose.SelectedValue = model.Purpose;
                ddlDName.SelectedValue = model.DName;
                txtDPerson.Text = model.DPerson;
                txtLinkMan.Text = model.LinkMan;
                txtDOContactInformation.Text = model.ContactInformation;
                txtDOAddress.Text = model.Address;
                txtDOMapCoordinates.Text = model.MapCoordinates;
                txtDExplain.Text = model.DExplain;
                ddlDOState.SelectedValue = model.State.ToString();
                ddlDOType.SelectedValue = model.Type;
                txtDOContent.Text = model.Content;
            }
            #endregion
        }
        #endregion
        #endregion

        #region Events
        /// <summary>
        /// 分配并关闭按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            string shid = GetQueryIntValue("shid");
            string doid = GetQueryIntValue("doid");

            #region 求助
            PartySeekHelp shmodel = new PartySeekHelp();

            if (shid != "")
            {
                shmodel = DB.PartySeekHelps.Find(shid);
                if (shmodel == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                if (shmodel.State != 1)
                {
                    Alert.Show("求助信息已操作！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                if (shmodel.EffectiveDate < Convert.ToDateTime(DateTime.Now.ToShortDateString()))
                {
                    Alert.Show("求助信息已过期！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }

                shmodel.State = 2;//已预约
            }
            #endregion

            #region 捐助
            PartyDonation domodel = new PartyDonation();
            if (doid != "")
            {
                domodel = DB.PartyDonations.Find(doid);
                if (domodel == null)
                {
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                if (domodel.State != 1)
                {
                    Alert.Show("捐助信息已操作！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                if (domodel.EffectiveDate < Convert.ToDateTime(DateTime.Now.ToShortDateString()))
                {
                    Alert.Show("捐助信息已过期！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                domodel.HelpId = shmodel.ID;
                domodel.State = 3;//已捐助
            }
            #endregion

            DB.SaveChanges();

            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

    }
}